package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JFrame;

import ocsf.client.AbstractClient;
import GUI.MainFrame;

public class ClientConsole
{
	  final public static int DEFAULT_PORT = 5555;
      public static MainFrame mf;
      public static ObservableClient client;  

     /**
  	 * constructs a new Client Console
  	 * @param host given host 
  	 * @param port given port
  	 * @param clientUI main frame of the program
  	 * @throws IOException
  	 */
    public ClientConsole (String host, int port, MainFrame clientUI) throws IOException 
    {    
    	try 
        {           
            client = new ObservableClient(host, port, clientUI);  
            
        } 
        catch (IOException exception) 
        {
            System.out.println("Error: Can't setup connection!" + " Terminating client.");
            System.exit(1);
        }
    }
    /**
     * receives a message from the client and sends it to the specific Observable Client
     * @param msg message received from the client
     */
    public void accept(ArrayList<Object> msg) 
    {                   
            try   
            { 
                client.handleMessageFromClientUI(msg);          
        } 
        catch (Exception ex) 
        {
          System.out.println("Unexpected error while reading from console!");
        }       
    }
    /**
     * Creates a new MainFrame
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException 
    {       
        mf = new MainFrame();
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        mf.setVisible(true);           
    }       
}